package org.liu.async.kit;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * Created by liuj on 2015/12/13.
 * 异步执行时,返回对象的future及其超时时间记录的对象
 */
public class ObjectFuture {

    /**
     * 记录异步执行结果的对象
     */
    private Future future;
    /**
     * 异步执行的超时时间
     */
    private long timeout;
    /**
     *
     * 超时时间的单位
     */
    private TimeUnit unit;

    public ObjectFuture(Future future, long timeout, TimeUnit unit) {
        this.future = future;
        this.timeout = timeout;
        this.unit = unit;
    }

    public Object get() throws ExecutionException, InterruptedException, TimeoutException {
        if (this.timeout == 0) {
            return future.get();
        } else {
            return future.get(timeout, unit);
        }
    }

    public Future getFuture() {
        return future;
    }

    public void setFuture(Future future) {
        this.future = future;
    }

    public long getTimeout() {
        return timeout;
    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    public TimeUnit getUnit() {
        return unit;
    }

    public void setUnit(TimeUnit unit) {
        this.unit = unit;
    }
}
